/**
 * 登录页面逻辑处理
 */

import * as account from '../../../js/modules/account.js'
import authorize from '../../../js/modules/authorize.js'
import {
  config
} from '../../../js/modules/gloab';
import {
  api,storage,dialog, util
} from '../../../js/util'
import {
  router
} from '../../../js/router'
import BasePage from '../../../js/common/base-page'
var app = getApp();
BasePage({
  pageKey: "login",
  validateLogin:false,
  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    role: "store", // 当前页面登录角色
    logo: "",
    bg_pic: "",
    logoHeight:80
  },

  /**微信一键授权登录 */
  onBindPhone: function(e) {
    // console.log(e,'=========',this.data.isExcuting);
    var that = this;
    if (that.data.isExcuting) return;
    if (e.detail.errMsg.indexOf("getPhoneNumber:ok") == -1) {
      return;
    }
    that.setData({
      isExcuting: true
    });
    var userInfo =util.mergeJsonObject(e.detail || {},account.login.authInfo);   
    userInfo["qr"]=that.data.qr;
    userInfo["code"]=this.data.code;
    account.login.onAuthLoginVerify(that.data.role, userInfo, function(res) {
      //展示弹出层
      that.setData({
        supplier: {
          "display": 'block',
          list: res.Content,
          isExcuting: false
        }
      });
    }, (bo, token, virtualPath) => {

      this.success(bo, token, virtualPath);
      that.setData({
        isExcuting: false
      });
  
    }, this);
  },
 
  /**登录成功后 */
  success(bo, token, virtualDir) {
    if (bo) {
      let role=authorize.loginRole();
      if(role=="store"){
        router.switchTab({
          path: "index",
        });
        return;
      }
      let backUrl = this.data.backUrl||"";
      backUrl = backUrl == "undefined" ? "" : backUrl;

      backUrl=(role=="supplier"?"#/supplier/admin":"#/clerk");

      router.redirect(
        {
          path: "pages/common/container/index",
          params: {
            action: "skip",
            token: token,
            role: role,
            hashPath: backUrl
          }
        });
    }
    else if(token==40029){
      wx.login({
        success: res =>{
            this.data.code = res.code
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { 
    let that = this;
    var role = "store",
        scene = storage.get('scene')||'1_0_0';
    if (options.role) {
      role = options.role;
    }
    else{
      role=authorize.loginRole();
    }
    this.data.role=role;
    this.setData({
      role:role,
      qr:scene
    });
    this.get_website_config();
    wx.login({
      success: res =>{
          this.data.code = res.code
      }
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.setNavigationBarTitle({
      title: api.title
    });

  },

  /**加载站点信息 */
  get_website_config() {
    var that = this;
    let {
      logo,bg_pic,imgDomain
    } = this.data;
    if (app.dns.proprietorId != app.dns.fkId) return;
    that.setData({
      logo: api.webSite.logo.length > 0 ? api.webSite.logo : logo,
      bg_pic: api.webSite.bgPic.length > 0 ? api.webSite.bgPic : bg_pic,
    });
  },
  /**logo自定义 */
  logoLoad(e){
    if(this.data.logo=='') return;
    var imgwidth = e.detail.width,
        imgheight = e.detail.height;
    var logoHeight = 440*imgheight/imgwidth;
    this.setData({
      logoHeight:logoHeight>200?200:logoHeight
    })
  },
  /**获得配置 */
  getconfigs: function() {
    var that = this;
    config.getConfig(
      app.dns.proprietorId,
      app.dns.proprietor,
      'Roles',
      'StoreSetting',
      function(res) {
        if (res && res.Success) {
          var content = res.Content;
          var wxRegisterAndLogin = content.WXRegisterAndLogin;

          that.setData({
            wxRegisterAndLogin: wxRegisterAndLogin,
            enableJoin: content.EnableJoin
          });
        }
      });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      independentPackage: app.dns.proprietorId > 0
    });
    this.getconfigs();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 显示遮罩层
   */
  showModal: function (evt) {
    dialog.showModal(this, 'showModalStatus')
  },
  /**
   * 隐藏遮罩层
   */
  hideModal: function () {
    dialog.hideModal(this, 'showModalStatus')
  }
});